package com.hw.dagl.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.hw.dagl.model.Sys_User;



public class BaseController {
	/**
	 * Spring MVC 自定义字段类�?
	 * @param request
	 * @param binder
	 * @throws Exception
	 */
	@InitBinder
	protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
		BinderDate(binder);
	}
	
	/**
	 * 转换时间格式
	 * @param binder
	 */
	protected void BinderDate(ServletRequestDataBinder binder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor editor = new CustomDateEditor(df, false);
		binder.registerCustomEditor(Date.class, editor);
	}
	
	
	/**
	 * Excute upload file,return file path 生成随机数名的jpg文件保存在desDic下�?
	 * @param request
	 * @param file
	 * @param desDic 上传目录
	 * @return
	 */
	protected String updateFile(HttpServletRequest request,MultipartFile multipartFile,String  desDic) {
		StringBuffer  random =  new StringBuffer(RandomStringUtils.randomAlphabetic(25));
		File file = new File(desDic);
		if(!file.exists()) {
			file.mkdirs();
		}
		String name = multipartFile.getOriginalFilename();
		if(StringUtils.isEmpty(name)) return null;
		name = name.substring(name.lastIndexOf("."));
		random.append(name);
		file =new File(desDic+random);
		
		try {
			FileUtils.writeByteArrayToFile(file, multipartFile.getBytes());
			return random.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Excute upload file,return file path 生成随机数名的jpg文件保存在desDic下�?
	 * @param request
	 * @param file
	 * @param desDic 上传目录
	 * @return
	 */
	protected String updateFile(HttpServletRequest request,FileItem item,String  desDic) {
		StringBuffer  random =  new StringBuffer(RandomStringUtils.randomAlphabetic(25));
		File file = new File(desDic);
		if(!file.exists()) {
			file.mkdirs();
		}
		String name = item.getName();
		if(StringUtils.isEmpty(name)) return null;
		name = name.substring(name.lastIndexOf("."));
		random.append(name);
		file =new File(desDic+random);
		
		try {
			item.write(file);
			return random.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	protected void exportExcel(HttpServletRequest request,HttpServletResponse response,String filename) throws UnsupportedEncodingException{
		response.addHeader("Content-Type","application/force-download ");
		filename = java.net.URLEncoder.encode(filename, "UTF-8");
		response.addHeader("Content-Disposition", "attachment;filename="+filename+".xls");
		//response.setContentType("application/vnd.ms-excel;charset=UTF-8");
		response.setContentType("application/octet-stream;charset=UTF-8");//解决在弹出文件下载框不能打开文件的问�?
	}
	
	protected void download(String path, HttpServletResponse response) {  
        try {  
            // path是指欲下载的文件的路径�?  
            File file = new File(path);  
            // 取得文件名�?  
            String filename = file.getName();  
            // 以流的形式下载文件�?  
            InputStream fis = new BufferedInputStream(new FileInputStream(path));  
            byte[] buffer = new byte[fis.available()];  
            fis.read(buffer);  
            fis.close();  
            // 清空response  
            response.reset();  
            // 设置response的Header  
            response.addHeader("Content-Disposition", "attachment;filename="  
                    + new String(filename.getBytes()));  
            response.addHeader("Content-Length", "" + file.length());  
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());  
            response.setContentType("application/vnd.ms-excel;charset=gb2312");  
            toClient.write(buffer);  
            toClient.flush();  
            toClient.close();  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }  
    }  
	
	@RequestMapping(method=RequestMethod.GET)
	public String home(HttpServletRequest request,HttpServletResponse response){
		return "home";
	}
}
